<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__check">
      <wms-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()">{{'新增盘点'}}</el-button>
          <el-button type="primary" @click="confirmCheckHandle()">{{'盘点出库'}}</el-button>
          <el-button type="primary" @click="cancelCheckHandle()">{{'取消盘点'}}</el-button>
        </el-form-item>
      </wms-form>
      <wms-table ref="wmsTable" class="checkTable" v-model="dataForm" :page="page" :total="total" :limit="limit"
        :tableData="dataList" :table-column="column" :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading" :showSelection="true" @selectChangeHandle="dataListSelectionChangeHandle"
        @pageSizeChangeHandle="pageSizeChangeHandle" @pageCurrentChangeHandle="pageCurrentChangeHandle" />
         <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./components/check-add-or-update";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wmsTable from '@/components/wms-table'
import wmsForm from '@/components/wms-form'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/task/page",
        getDataListIsPage: true,
        exportURL: "/wms/check/export",
        deleteURL: "/wms/check",
        deleteIsBatch: true,
        deleteIsBatchKey: 'checkId'
      },
      dataForm: {
        taskType: '05',
        spuNo: '',
        spuName: '',
        contNo: '',
        curPosition: ''
      },
      changeForm: {
        taskId: ''
      },
      taskIds:[],
      formList,
      tableColumn,
      column: [],
      advanced: false
    };
  },
  components: {
    AddOrUpdate,
    wmsTable,
    wmsForm
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    dataListSelectionChangeHandle(selection, row) {
      this.dataForm.taskId = ''; //每次单选都重置id
      // if (selection.length > 1) {
      //   this.$refs['wmsTable'].$refs.Table.clearSelection()
      //   this.$refs['wmsTable'].$refs.Table.toggleRowSelection(row)
      // }
      // if (selection.length != 0) {
      //   //只有当真正选中了目标主单才获取id
      //   this.dataForm.taskId = row.taskId;
      // }
      this.taskIds = selection.map(item => item.taskId)
    },
    // 处理表格多选
    handleSelectionChange(selection) {
      this.taskIds = selection.map(item => item.taskId)
    },
    // 盘点出库
    async confirmCheckHandle() {
      if (this.taskIds.length === 0) {
        return this.$message.warning('请至少选择一条数据')
      }
      try {
        const { data: res } = await this.$http.post('/wms/stock/outbound', {
          ids: this.taskIds,
        })
        console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        } else {
          this.taskIds = null;
          this.query();
        }
        this.$message.success(`出库成功`)
        this.getDataList() // 刷新数据
      } catch (err) {
        console.error('操作失败:', err)
      }
    },
    // 盘点取消
    async cancelCheckHandle() {
      if (this.taskIds.length === 0) {
        return this.$message.warning('请至少选择一条数据')
      }
      try {
        const { data: res } = await this.$http.post('/wms/stock/cancel', {
          ids: this.taskIds,
        })
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        } else {
          this.taskIds = null;
          this.query();
        }
        console.log(res)
        this.$message.success(`取消成功`)
        this.getDataList() // 刷新数据
      } catch (err) {
        console.error('操作失败:', err)
      }
    },
    // //1.盘点出库
    // confirmCheckHandle: debounce(function (val, row) {
    //   this.changeForm.taskId = this.dataForm.taskId;
    //   this.$http.post('/wms/stock/outbound', this.changeForm).then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }else {
    //       this.changeForm.taskId = null;
    //       this.query();
    //     }
    //   }).catch(() => { })
    // }),
    //1.盘点取消
    // cancelCheckHandle: debounce(function (val, row) {
    //   this.changeForm.taskId = this.dataForm.taskId;
    //   this.$http.post('/wms/stock/cancel', this.changeForm).then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     } else{
    //       this.changeForm.taskId = null;
    //       this.query();
    //     }
    //   }).catch(() => { })
    //
    // })
  }
}
</script>
<!--<style scoped lang="scss">-->
<!--/* 隐藏表头的全选框 */-->
<!--::v-deep .checkTable thead .el-table-column&#45;&#45;selection .cell {-->
<!--  display: none;-->
<!--}-->
<!--</style>-->
